var vm = new Vue({
    el:"#app",
    data:{
        staffInfo: {
            name:''
        },
        agentinfo: {
            scheduleDate:'',
            scheduleDeletetime:''
        },

        agented:[],
        staffedId:0
    },
    methods:{
        //渲染日程安排所有人的名字
        getStaffName:function(){
            axios.post("http://localhost:8099/PersonalInfo/getSelfInfo",{id:1}).then(
                function(result){
                    console.log(result.data);
                    vm.staffInfo=result.data;
                },function (err) {
                    console.log(err);
                }
            )
        },
        //渲染当前日程安排的信息
        getAgentInfo:function(){
            axios.post("http://localhost:8099/WorkSchedule/updateScheduleInfo").then(
                function(result){
                    console.log(result.data);
                    vm.agentinfo=result.data;
                },function (err) {
                    console.log(err);
                }
            )
        },
        //渲染可以替他人提交日程的员工
        getAgentToOthers:function(){
            axios.post("http://localhost:8099/WorkSchedule/getAgentToOthers").then(
                function(result){
                    console.log(result.data);
                    vm.agented=result.data;
                },function (err) {
                    console.log(err);
                }
            )
        },
        //提交修改日程安排的事务前检测时间冲突
        submitBtn:function(){
            var scheduleTitle = $("#scheduleTitle").val();
            var scheduleText = $("#scheduleText").val();
            var scheduleStartTime = $("#scheduleStartTime").val();
            var scheduleEndTime = $("#scheduleEndTime").val();
            var staffId = $("#scheduleStaff").attr("staffId");

            console.log(vm.staffedId);
            if(scheduleText===''||scheduleTitle===''){
                alert("日程安排正文、日程安排标题不可为空！");
            }else if(vm.staffedId==0){
                alert("请选择被授予人！")
            }else{
                scheduleEndTime = scheduleEndTime.replace("T"," ");
                scheduleStartTime = scheduleStartTime.replace("T"," ");
                if(scheduleEndTime>scheduleStartTime){
                    axios.post(
                        "http://localhost:8099/WorkSchedule/CheckTime",
                        {
                            scheduleDate:scheduleStartTime,
                            scheduleDeletetime:scheduleEndTime,
                            scheduleEmpedId:vm.staffedId
                        }
                    ).then(
                        function (result) {
                            if(result.data===true){
                                axios.post(
                                    "http://localhost:8099/WorkSchedule/updateSchedule",
                                    {
                                        scheduleText:scheduleText,
                                        scheduleDate:scheduleStartTime,
                                        scheduleEmpId:staffId,
                                        scheduleTitle:scheduleTitle,
                                        scheduleDeletetime:scheduleEndTime,
                                        scheduleEmpedId:vm.staffedId
                                    }
                                ).then(
                                    function (result) {
                                        if(result.data===true){
                                            alert("日程修改成功！");

                                        }else{
                                            alert("!!!");
                                        }
                                    },function (err) {
                                        console.log(err);
                                    }
                                )
                            }else{
                                alert("时间冲突!");
                            }
                        }
                    )
                }else{
                    alert("日程安排时间设置有误！")
                }
            }

        },
        //失去被授予人下拉框焦点，获取被授予人id
        blurGetStaffId:function () {
            vm.staffedId = $("#scheduleStaffed>option:checked").attr("staffId");
        }
    },
    mounted(){
        this.getStaffName();
        this.getAgentToOthers();
        this.getAgentInfo();
    }
})